# -*- coding: utf-8 -*-
# ㊎ Author : XMZ
# ㊍ Time : 2021/5/5 16:25
# ㊌ File : RabbitMQ -> producer_fanout.py
# ㊋ IDE :  PyCharm
# ㊏ 
import os
import random
import sys
import time

import pika

# 登录信息
hosts = "10.0.2.22"
name = "admin"
pwd = "123456"
# 获 取连接RabbitMQ的用户名和密码 对象
credential = pika.PlainCredentials(username=name, password=pwd)  # 连接对象
# 1. 获得与 RabbitMQ Server 的连接对象
connection = pika.BlockingConnection(
    pika.ConnectionParameters(
        host=hosts,
        credentials=credential,
        heartbeat=0,
    )
)
# 获得 channel 管道
channel = connection.channel()

for i in range(50):
    num = str(random.randint(0, 4))
    info = {
        '0': "info",
        '1': "debug",
        '2': "error",
        '3': "other",
        '4': "warning",
    }

    # 提交用户上传的消息
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    channel.basic_publish(exchange="log", routing_key=f"{info[num]}", body=f"{info[num]} ---- {t} - {i}")
connection.close()

